import ajax from './ajax';
import globalConfig from '../config';
const loginVerify = {
    checkLogin: function (callback) {
        var self = this;
        ajax.post({
            "url": '/user/getCurrentUser',
            "loginVerify": true,
            "success": function (res) {
                if (res.data == null) {
                    window.location.href = globalConfig.getAPIPath();
                } else {          
                    self.userInfo = res.data;      
                    if (callback) {
                        callback(res);
                    }
                }
            },
            "error": function (error) {

            }
        })
    },
    getUserDataInfo: function () {
        return this.userInfo;
    },
    getModulePermission: function (moduleName) {
      let permission=[];
      var permissionMap=this.userInfo.userRoles[0].role.permissionMap;
      if(permissionMap)
      {
          if(permissionMap[moduleName])
          {
              permission=permissionMap[moduleName];
          }
      }
    //   console.log(permission);
    //   console.log(moduleName);
      let arr = [...permission];
      arr.forEach((value, index) => {
        if(value === 'save') {
            if(index === 0){
                return;
            }
            const one = arr[0];
            arr[index] = one;
            arr[0] = 'save';
        }
      });
      // console.log('arr',arr);
    
      return arr;
        
    }

}

module.exports = loginVerify;
